import React, { createContext, useContext } from 'react';
import { notification, Button, Space } from 'antd';

const NotificationContext = createContext(null);

export const useNotificationService = () => useContext(NotificationContext);

const NotificationProvider = ({ children }) => {
    const [api, contextHolder] = notification.useNotification();

    const showNotification = ({
                                  type = 'info',
                                  message,
                                  description,
                                  key,
                                  onConfirm,
                                  onCancel,
                                  onClose,
                                  duration = null
                              }) => {
        const btn = (
            <Space>
                {onCancel && (
                    <Button type="link" size="small" onClick={() => {
                        api.destroy(key);
                        onCancel?.();
                    }}>
                        取消
                    </Button>
                )}
                {onConfirm && (
                    <Button type="primary" size="small" onClick={() => {
                        api.destroy(key);
                        onConfirm?.();
                    }}>
                        确认
                    </Button>
                )}
            </Space>
        );

        api[type]({
            message,
            description,
            btn,
            key,
            duration,
            onClose,
        });
    };

    return (
        <NotificationContext.Provider value={{ showNotification }}>
            {contextHolder}
            {children}
        </NotificationContext.Provider>
    );
};

export default NotificationProvider;
